package com.lyf.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lyf.core.dao.RoleDao;
import com.lyf.core.pojo.Role;
import com.lyf.core.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService{
	@Autowired
	private RoleDao roledao;
	
	
	public String save(Role r) {
		
		//判断名称是否重复
		if(r == null || r.getRole_name() == null || r.getRole_name().trim().equals("")) {
			return "角色名称为空" ;
		}
		Role _r = findByName(r.getRole_name());
		if(_r != null) {
			return "角色名称已存在" ;
		}
		 
		roledao.insert(r);
		
		return null;
	}
	
	public void delete(int r_id){
		roledao.delete(r_id);
	}
	
	public String update(Role r) {
		//判断名称是否重复
		if(r == null || r.getRole_id() == null || r.getRole_id() <= 0 || r.getRole_name() == null || r.getRole_name().trim().equals("")) {
			return "更新角色为空" ;
		}
		Role _r = findByName(r.getRole_name());
		if(_r != null) {
			return "角色名称已存在" ;
		}
		roledao.update(r);
		
		return null;
	}
	
	public List<Role> findAll() {
		return roledao.findAll();
	}
	
	public List<Role> findUserRoles(String user_id){
		  
		return roledao.findUserRoles(user_id) ;
	}
	
	
	public Role findByName(String roleName){
		return roledao.findByName(roleName);
	}

	@Override
	public void updateRolePerms(int role_id, String perms) {
		// TODO Auto-generated method stub
		
		/*Map<String, Menu> id_menu = CommonUtils.getMenuMap();
		
		//判断子权限中是否包含了父权限ID
		String[] _ps = perms.split(",");
		
		Set<String> parent_ids = new HashSet<>();
		
		//获取父级并判断是否包含 
		for(String _perm : _ps){
			Menu m = id_menu.get(_perm);
			if(m != null){
				parent_ids.add(m.getParent_id());
			}
		}
		
		//判断父级添加里面的更上级
		for(String _perm : parent_ids.toArray(new String[0])){
			Menu m = id_menu.get(_perm);
			if(m != null){
				parent_ids.add(m.getParent_id());
			}
		}
		
		parent_ids.addAll(Arrays.asList(_ps));
		
		StringBuilder sb = new StringBuilder();
		for(String s : parent_ids){
			sb.append(s + ",");
		}
		if(sb.length() > 1) {
			sb.deleteCharAt(sb.length() - 1);
		}
		
		perms = sb.toString();*/
		
		roledao.updateRolePerms(role_id, perms);
	}
	
}
